<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>综合服务器测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .section {
            margin: 20px 0;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .status {
            padding: 10px;
            margin: 10px 0;
            border-radius: 5px;
        }
        .success { background-color: #d4edda; color: #155724; }
        .error { background-color: #f8d7da; color: #721c24; }
        .info { background-color: #d1ecf1; color: #0c5460; }
        button {
            padding: 8px 16px;
            margin: 5px;
            border: none;
            border-radius: 4px;
            background-color: #007bff;
            color: white;
            cursor: pointer;
        }
        button:hover { background-color: #0056b3; }
        input, textarea {
            padding: 8px;
            margin: 5px;
            border: 1px solid #ddd;
            border-radius: 4px;
            width: 200px;
        }
        #chatMessages {
            height: 200px;
            overflow-y: auto;
            border: 1px solid #ddd;
            padding: 10px;
            background-color: #f9f9f9;
        }
    </style>
</head>
<body>
    <h1>综合服务器功能测试</h1>
    
    <div class="section">
        <h2>连接状态</h2>
        <div id="connectionStatus" class="status info">未连接</div>
        <button onclick="connect()">连接服务器</button>
        <button onclick="disconnect()">断开连接</button>
    </div>

    <div class="section">
        <h2>用户登录</h2>
        <input type="text" id="userId" placeholder="用户ID" value="testuser1">
        <button onclick="login()">登录</button>
        <div id="loginStatus" class="status info">未登录</div>
    </div>

    <div class="section">
        <h2>聊天功能</h2>
        <input type="text" id="chatTo" placeholder="发送给谁" value="testuser2">
        <input type="text" id="chatMessage" placeholder="消息内容">
        <button onclick="sendMessage()">发送消息</button>
        <div id="chatMessages"></div>
    </div>

    <div class="section">
        <h2>WebRTC 通话</h2>
        <input type="text" id="callTo" placeholder="呼叫谁" value="testuser2">
        <button onclick="startCall()">开始通话</button>
        <button onclick="endCall()">结束通话</button>
        <div id="callStatus" class="status info">未通话</div>
    </div>

    <script src="https://cdn.socket.io/4.8.1/socket.io.min.js"></script>
    <script>
        let socket = null;
        let currentUser = null;

        function connect() {
            try {
                socket = io('https://192.168.43.82:3000', {
                    transports: ['websocket'],
                    rejectUnauthorized: false
                });

                socket.on('connect', () => {
                    document.getElementById('connectionStatus').innerHTML = '已连接';
                    document.getElementById('connectionStatus').className = 'status success';
                });

                socket.on('disconnect', () => {
                    document.getElementById('connectionStatus').innerHTML = '已断开';
                    document.getElementById('connectionStatus').className = 'status error';
                });

                socket.on('login-success', (data) => {
                    currentUser = data.id;
                    document.getElementById('loginStatus').innerHTML = `已登录: ${data.id}`;
                    document.getElementById('loginStatus').className = 'status success';
                });

                socket.on('login-fail', (error) => {
                    document.getElementById('loginStatus').innerHTML = `登录失败: ${error}`;
                    document.getElementById('loginStatus').className = 'status error';
                });

                socket.on('new-message', (data) => {
                    addChatMessage(data.from, data.message, data.timestamp);
                });

                socket.on('call-made', (data) => {
                    document.getElementById('callStatus').innerHTML = `收到来自 ${data.from} 的呼叫`;
                    document.getElementById('callStatus').className = 'status info';
                    if (confirm(`收到来自 ${data.from} 的呼叫，是否接听？`)) {
                        socket.emit('accept-call', { to: data.from, signal: 'accept' });
                    } else {
                        socket.emit('reject-call', { to: data.from });
                    }
                });

                socket.on('call-accepted', (data) => {
                    document.getElementById('callStatus').innerHTML = '通话已接通';
                    document.getElementById('callStatus').className = 'status success';
                });

                socket.on('call-rejected', () => {
                    document.getElementById('callStatus').innerHTML = '通话被拒绝';
                    document.getElementById('callStatus').className = 'status error';
                });

                socket.on('callEnded', () => {
                    document.getElementById('callStatus').innerHTML = '通话已结束';
                    document.getElementById('callStatus').className = 'status info';
                });

            } catch (error) {
                document.getElementById('connectionStatus').innerHTML = `连接失败: ${error.message}`;
                document.getElementById('connectionStatus').className = 'status error';
            }
        }

        function disconnect() {
            if (socket) {
                socket.disconnect();
                socket = null;
                currentUser = null;
            }
        }

        function login() {
            if (!socket) {
                alert('请先连接服务器');
                return;
            }
            const userId = document.getElementById('userId').value;
            if (!userId) {
                alert('请输入用户ID');
                return;
            }
            socket.emit('login', { id: userId });
        }

        function sendMessage() {
            if (!socket || !currentUser) {
                alert('请先连接并登录');
                return;
            }
            const to = document.getElementById('chatTo').value;
            const message = document.getElementById('chatMessage').value;
            if (!to || !message) {
                alert('请输入接收者和消息内容');
                return;
            }

            // 发送到服务器
            fetch('/chat', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    from: currentUser,
                    to: to,
                    message: message
                })
            })
            .then(response => response.json())
            .then(data => {
                if (data.code === 200) {
                    addChatMessage(currentUser, message, new Date());
                    document.getElementById('chatMessage').value = '';
                } else {
                    alert('发送失败: ' + data.msg);
                }
            })
            .catch(error => {
                alert('发送失败: ' + error.message);
            });
        }

        function addChatMessage(from, message, timestamp) {
            const chatMessages = document.getElementById('chatMessages');
            const time = new Date(timestamp).toLocaleTimeString();
            const div = document.createElement('div');
            div.innerHTML = `<strong>${from}</strong> (${time}): ${message}`;
            chatMessages.appendChild(div);
            chatMessages.scrollTop = chatMessages.scrollHeight;
        }

        function startCall() {
            if (!socket || !currentUser) {
                alert('请先连接并登录');
                return;
            }
            const to = document.getElementById('callTo').value;
            if (!to) {
                alert('请输入呼叫对象');
                return;
            }
            socket.emit('call-user', {
                userToCall: to,
                signalData: 'call-signal',
                from: currentUser
            });
            document.getElementById('callStatus').innerHTML = '正在呼叫...';
            document.getElementById('callStatus').className = 'status info';
        }

        function endCall() {
            if (socket) {
                socket.emit('endCall', 'room1');
                document.getElementById('callStatus').innerHTML = '通话已结束';
                document.getElementById('callStatus').className = 'status info';
            }
        }

        // 页面加载时自动连接
        window.onload = function() {
            connect();
        };
    </script>
</body>
</html> 